package client

import (
	"log"
	"time"

	socketio_client "github.com/zhouhui8915/go-socket.io-client"
)

//var counter int = 0

func ClientSocket() {
	opts := &socketio_client.Options{
		Transport: "websocket",
		Query:     make(map[string]string),
	}
	opts.Query["uid"] = "111"
	opts.Query["cid"] = "conf_123"
	uri := "http://192.168.139.23:8000"
	//uri := "http://127.0.0.1:8000"

	client, err := socketio_client.NewClient(uri, opts)
	if err != nil {
		log.Println("NewClient failed: ", err)
		return
	}

	client.On("connection", func() {
		log.Println("connection success!")
	})

	client.On("error", func() {
		log.Println("have error!")
	})

	client.On("disconnection", func() {
		log.Println("disconnect.........")
	})

	client.On("message", func(msg string) {
		log.Println("massage is : ", msg)
	})

	client.On("reply", func(msg string) {
		log.Println("reply massage is : ", msg)
	})

	authStr := "client " + " {\"uid\":\"\"" + opts.Query["uid"] + "\"cid\":\"" + opts.Query["cid"] + "\"}"

	for {
		err := client.Emit("authenticate", authStr)
		if err != nil {
			log.Printf("Emit auth error:%v\n", err)
		}
		time.Sleep(1 * time.Second)
	}

}
